// Test file for hash calculation functionality
// This file can be used to test the new hash calculation system

import { calculateFileHash, calculateFileHashWithProgress } from './hashUtils';

/**
 * Test hash calculation with different file sizes
 */
export async function testHashCalculation(): Promise<void> {
  console.log('🧪 开始测试hash计算功能...');

  // Test 1: Small file (should use main thread)
  const smallFile = new File(['Hello, World!'], 'test.txt', { type: 'text/plain' });
  console.log('📝 测试小文件 (主线程计算)...');
  
  const smallFileHash = await calculateFileHash(smallFile, { useWorker: false });
  console.log(`✅ 小文件hash: ${smallFileHash}`);

  // Test 2: Large file simulation (should use worker)
  const largeContent = new Array(1024 * 1024).fill('A').join(''); // 1MB content
  const largeFile = new File([largeContent], 'large.txt', { type: 'text/plain' });
  console.log('📝 测试大文件 (Worker计算)...');
  
  const largeFileHash = await calculateFileHashWithProgress(
    largeFile,
    (progress) => {
      console.log(`📊 大文件hash计算进度: ${progress.toFixed(1)}%`);
    },
    { useWorker: true, chunkSize: 256 * 1024 } // 256KB chunks
  );
  console.log(`✅ 大文件hash: ${largeFileHash}`);

  // Test 3: Test with progress callback
  console.log('📝 测试进度回调...');
  const mediumContent = new Array(512 * 1024).fill('B').join(''); // 512KB content
  const mediumFile = new File([mediumContent], 'medium.txt', { type: 'text/plain' });
  
  await calculateFileHashWithProgress(
    mediumFile,
    (progress) => {
      console.log(`📊 中等文件hash计算进度: ${progress.toFixed(1)}%`);
    }
  );

  console.log('🎉 所有hash计算测试完成!');
}

/**
 * Test hash calculation in different environments
 */
export async function testEnvironmentCompatibility(): Promise<void> {
  console.log('🌐 测试环境兼容性...');

  // Test HTTPS environment (should use crypto.subtle)
  if (typeof crypto !== 'undefined' && crypto.subtle) {
    console.log('✅ 检测到crypto.subtle支持 (HTTPS环境)');
  } else {
    console.log('⚠️ 未检测到crypto.subtle支持 (HTTP环境，将使用fallback实现)');
  }

  // Test Web Worker support
  if (typeof Worker !== 'undefined') {
    console.log('✅ 检测到Web Worker支持');
  } else {
    console.log('⚠️ 未检测到Web Worker支持');
  }

  // Test with a small file to verify basic functionality
  const testFile = new File(['Test content'], 'test.txt', { type: 'text/plain' });
  try {
    const hash = await calculateFileHash(testFile);
    console.log(`✅ 基础hash计算测试通过: ${hash}`);
  } catch (error) {
    console.error('❌ 基础hash计算测试失败:', error);
  }
}

/**
 * Performance test for hash calculation
 */
export async function testHashPerformance(): Promise<void> {
  console.log('⚡ 开始性能测试...');

  const sizes = [1024, 1024 * 1024, 5 * 1024 * 1024]; // 1KB, 1MB, 5MB
  const results: Array<{ size: number; time: number; method: string }> = [];

  for (const size of sizes) {
    const content = new Array(size).fill('X').join('');
    const file = new File([content], `test_${size}.txt`, { type: 'text/plain' });

    // Test main thread
    const startMain = performance.now();
    await calculateFileHash(file, { useWorker: false });
    const timeMain = performance.now() - startMain;
    results.push({ size, time: timeMain, method: 'main-thread' });

    // Test worker (if file is large enough)
    if (size > 1024 * 1024) {
      const startWorker = performance.now();
      await calculateFileHash(file, { useWorker: true });
      const timeWorker = performance.now() - startWorker;
      results.push({ size, time: timeWorker, method: 'worker' });
    }
  }

  console.log('📊 性能测试结果:');
  results.forEach(result => {
    const sizeMB = (result.size / 1024 / 1024).toFixed(2);
    console.log(`  ${result.method}: ${sizeMB}MB - ${result.time.toFixed(2)}ms`);
  });
}

// Export test functions for use in development
export const hashTests = {
  testHashCalculation,
  testEnvironmentCompatibility,
  testHashPerformance
};
